/*
 * Copyright (c) 2013, Ford Motor Company All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met: ·
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer. · Redistributions in binary
 * form must reproduce the above copyright notice, this list of conditions and
 * the following disclaimer in the documentation and/or other materials provided
 * with the distribution. · Neither the name of the Ford Motor Company nor the
 * names of its contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
/*
 * Reference implementation of UI component.
 *
 * Interface to get or set some essential information sent from SDLCore. UI is
 * responsible for the functionality provided by the application: display
 * graphics and multimedia components, is responsible for the transfer of
 * managed manipulations, generated by the user to the server.
 *
 */

FFW.UI = FFW.RPCObserver.create({

    /**
     * If true then UI is present and ready to communicate with SDL.
     *
     * @type {Boolean}
     */
    isReady: true,

    /**
     * Contains response codes for request that should be processed but there were some kind of errors
     * Error codes will be injected into response.
     */
    errorResponsePull: {},

    /**
     * access to basic RPC functionality
     */
    client: FFW.RPCClient.create({
        componentName: "UI"
    }),

    // temp var for debug
    appID: 1,

    onRecordStartSubscribeRequestID: -1,

    onRecordStartUnsubscribeRequestID: -1,

    // const
    onRecordStartNotification: "UI.OnRecordStart",

    /**
     * ids for requests AudioPassThru
     */
    performAudioPassThruRequestID: -1,
    endAudioPassThruRequestID: -1,

    /**
     * connect to RPC bus
     */
    connect: function () {

        this.client.connect(this, 400); // Magic number is unique identifier
        // for component
    },

    /**
     * disconnect from RPC bus
     */
    disconnect: function () {

        this.onRPCUnregistered();
        this.client.disconnect();
    },

    /**
     * Client is registered - we can send request starting from this point
     * of time
     */
    onRPCRegistered: function () {

        Em.Logger.log("FFW.UI.onRPCRegistered");
        this._super();

        // subscribe to notifications
        this.onRecordStartSubscribeRequestID = this.client.subscribeToNotification(this.onRecordStartNotification);
    },

    /**
     * Client is unregistered - no more requests
     */
    onRPCUnregistered: function () {

        Em.Logger.log("FFW.UI.onRPCUnregistered");
        this._super();

        // unsubscribe from notifications
        this.onRecordStartUnsubscribeRequestID = this.client.unsubscribeFromNotification(this.onRecordStartNotification);
    },

    /**
     * Client disconnected.
     */
    onRPCDisconnected: function () {

    },

    /**
     * when result is received from RPC component this function is called It
     * is the propriate place to check results of request execution Please
     * use previously store reuqestID to determine to which request repsonse
     * belongs to
     */
    onRPCResult: function (response) {

        Em.Logger.log("FFW.UI.onRPCResult");
        this._super();
    },

    /**
     * handle RPC erros here
     */
    onRPCError: function (error) {

        Em.Logger.log("FFW.UI.onRPCError");
        this._super();
    },

    /**
     * handle RPC notifications here
     */
    onRPCNotification: function (notification) {

        Em.Logger.log("FFW.UI.onRPCNotification");
        this._super();

        if (notification.method == this.onRecordStartNotification) {
            SDL.AudioPassThruPopUp.StartAudioPassThruTimer();
        }
    },

    /**
     * handle RPC requests here
     */
    onRPCRequest: function (request) {

        Em.Logger.log("FFW.UI.onRPCRequest");

        if (this.validationCheck(request)) {

            switch (request.method) {
                case "UI.Alert":
                {

                    // Werify if there is an ansupported data in request
                    if (this.errorResponsePull[request.id] != null) {

                        //Check if there is any available data to  process the request
                        if (request.params.alertStrings.length > 0
                            || "softButtons" in request.params) {

                            this.errorResponsePull[request.id].code = SDL.SDLModel.resultCode["WARNINGS"];
                        } else {
                            //If no available data sent error response and stop process current request

                            this.sendError(this.errorResponsePull[request.id].code, request.id, request.method,
                                    "Unsupported " + this.errorResponsePull[request.id].type + " type. Request was not processed.");
                            this.errorResponsePull[request.id] = null;

                            return;
                        }
                    }

                    if (SDL.SDLModel.onUIAlert(request.params, request.id)) {
                        SDL.SDLController.onSystemContextChange(request.params.appID);
                    }

                    break;
                }
                case "UI.Show":
                {

                    // Werify if there is an ansupported data in request
                    if (this.errorResponsePull[request.id] != null) {

                        //Check if there is any available data to  process the request
                        if (request.params.showStrings.length > 0
                            || "graphic" in request.params
                            || "secondaryGraphic" in request.params
                            || "softButtons" in request.params
                            || "customPresets" in request.params) {

                            this.errorResponsePull[request.id].code = SDL.SDLModel.resultCode["WARNINGS"];
                        } else {
                            //If no available data sent error response and stop process current request

                            this.sendError(this.errorResponsePull[request.id].code, request.id, request.method,
                            "Unsupported " + this.errorResponsePull[request.id].type + " type. Request was not processed.");
                            this.errorResponsePull[request.id] = null;

                            return;
                        }
                    }

                    SDL.TurnByTurnView.deactivate();
                    SDL.SDLController.getApplicationModel(request.params.appID).onSDLUIShow(request.params);
                    this.sendUIResult(SDL.SDLModel.resultCode["SUCCESS"], request.id, request.method);

                    break;
                }
                case "UI.SetGlobalProperties":
                {

                    // Werify if there is an ansupported data in request
                    if (this.errorResponsePull[request.id] != null) {

                        //Check if there is any available data to  process the request
                        if ("menuTitle" in request.params
                            || "keyboardProperties" in request.params
                            || "vrHelp" in request.params
                            || "menuIcon" in request.params) {

                            this.errorResponsePull[request.id].code = SDL.SDLModel.resultCode["WARNINGS"];
                        } else {
                            //If no available data sent error response and stop process current request

                            this.sendError(this.errorResponsePull[request.id].code, request.id, request.method,
                                    "Unsupported " + this.errorResponsePull[request.id].type + " type. Request was not processed.");
                            this.errorResponsePull[request.id] = null;

                            return;
                        }
                    }

                    SDL.SDLModel.setProperties(request.params);

                    this.sendUIResult(SDL.SDLModel.resultCode["SUCCESS"], request.id, request.method);

                    break;
                }
                case "UI.AddCommand":
                {

                    // Werify if there is an ansupported data in request
                    if (this.errorResponsePull[request.id] != null) {

                        //Check if there is any available data to  process the request
                        if ("cmdIcon" in request.params
                            || "menuParams" in request.params) {

                            this.errorResponsePull[request.id].code = SDL.SDLModel.resultCode["WARNINGS"];
                        } else {
                            //If no available data sent error response and stop process current request

                            this.sendError(this.errorResponsePull[request.id].code, request.id, request.method,
                                    "Unsupported " + this.errorResponsePull[request.id].type + " type. Request was not processed.");
                            this.errorResponsePull[request.id] = null;

                            return;
                        }
                    }

                    SDL.SDLController.getApplicationModel(request.params.appID).addCommand(request);

                    break;
                }
                case "UI.DeleteCommand":
                {

                    SDL.SDLController.getApplicationModel(request.params.appID).deleteCommand(request.params.cmdID, request.id);

                    break;
                }
                case "UI.AddSubMenu":
                {

                    SDL.SDLController.getApplicationModel(request.params.appID).addSubMenu(request);

                    break;
                }
                case "UI.DeleteSubMenu":
                {

                    var resultCode = SDL.SDLController.getApplicationModel(request.params.appID).deleteSubMenu(request.params.menuID);
                    this.sendUIResult(resultCode, request.id, request.method);

                    break;
                }
                case "UI.PerformInteraction":
                {

                    // Werify if there is an ansupported data in request
                    if (this.errorResponsePull[request.id] != null) {

                        //Check if there is any available data to  process the request
                        if ("choiceSet" in request.params
                            && request.params
                            && request.params.interactionLayout != "KEYBOARD") {

                            this.errorResponsePull[request.id].code = SDL.SDLModel.resultCode["WARNINGS"];
                        } else {
                            //If no available data sent error response and stop process current request

                            this.sendError(this.errorResponsePull[request.id].code,
                                request.id,
                                request.method,
                                "Unsupported " + this.errorResponsePull[request.id].type + " type. Request was not processed.");
                            this.errorResponsePull[request.id] = null;

                            return;
                        }
                    }

                    if (SDL.SDLModel.uiPerformInteraction(request)) {
                        SDL.SDLController.onSystemContextChange();
                    }

                    break;
                }
                case "UI.SetMediaClockTimer":
                {

                    var resultCode = SDL.SDLController.getApplicationModel(request.params.appID).sdlSetMediaClockTimer(request.params);
                    if (resultCode === SDL.SDLModel.resultCode["SUCCESS"]) {
                        this.sendUIResult(resultCode, request.id, request.method);
                    } else {
                        this.sendError(resultCode,
                            request.id,
                            request.method,
                            'Request is ignored, because the intended result is already in effect.');
                    }

                    break;
                }
                case "UI.Slider":
                {

                    if (SDL.SDLModel.uiSlider(request)) {
                        SDL.SDLController.onSystemContextChange();
                    }

                    break;
                }
                case "UI.ScrollableMessage":
                {

                    if (SDL.SDLModel.onSDLScrolableMessage(request, request.id)) {
                        SDL.SDLController.onSystemContextChange();
                    }

                    break;
                }
                case "UI.ChangeRegistration":
                {

                    if (request.params.appName) {
                        SDL.SDLController.getApplicationModel(request.params.appID).set('appName',
                            request.params.appName);
                    }

                    SDL.SDLModel.changeRegistrationUI(request.params.language,
                        request.params.appID,
                        request.params.appName);
                    this.sendUIResult(SDL.SDLModel.resultCode["SUCCESS"], request.id, request.method);

                    break;
                }
                case "UI.SetDisplayLayout":
                {

                    var senResponseFlag = false;

                    switch (request.params.displayLayout){
                        case "MEDIA": {
                            senResponseFlag = true;
                            break;
                        }
                        case "NON-MEDIA": {
                            senResponseFlag = true;
                            break;
                        }
                        case "DEFAULT": {
                            senResponseFlag = true;
                            break;
                        }
                        case "ONSCREEN_PRESETS": {
                            senResponseFlag = true;
                            break;
                        }
                        case "NAV_FULLSCREEN_MAP": {
                            senResponseFlag = true;
                            break;
                        }
                        case "NAV_KEYBOARD": {
                            senResponseFlag = true;
                            break;
                        }
                        case "NAV_LIST": {
                            senResponseFlag = true;
                            break;
                        }
                    }

                    if (senResponseFlag) {

                        Em.Logger.log("FFW." + request.method + "Response");

                        // send repsonse
                        var JSONMessage = {
                            "jsonrpc": "2.0",
                            "id": request.id,
                            "result": {
                                "displayCapabilities": {
                                    "displayType": "GEN2_8_DMA",
                                    "textFields": [{
                                            "name": "mainField1",
                                            "characterSet": "TYPE2SET",
                                            "width": 500,
                                            "rows": 1
                                        },
                                        {
                                            "name": "mainField2",
                                            "characterSet": "TYPE2SET",
                                            "width": 500,
                                            "rows": 1
                                        },
                                        {
                                            "name": "mainField3",
                                            "characterSet": "TYPE2SET",
                                            "width": 500,
                                            "rows": 1
                                        },
                                        {
                                            "name": "mainField4",
                                            "characterSet": "TYPE2SET",
                                            "width": 500,
                                            "rows": 1
                                        },
                                        {
                                            "name": "statusBar",
                                            "characterSet": "TYPE2SET",
                                            "width": 500,
                                            "rows": 1
                                        },
                                        {
                                            "name": "mediaClock",
                                            "characterSet": "TYPE2SET",
                                            "width": 500,
                                            "rows": 1
                                        },
                                        {
                                            "name": "mediaTrack",
                                            "characterSet": "TYPE2SET",
                                            "width": 500,
                                            "rows": 1
                                        },
                                        {
                                            "name": "alertText1",
                                            "characterSet": "TYPE2SET",
                                            "width": 500,
                                            "rows": 1
                                        },
                                        {
                                            "name": "alertText2",
                                            "characterSet": "TYPE2SET",
                                            "width": 500,
                                            "rows": 1
                                        },
                                        {
                                            "name": "alertText3",
                                            "characterSet": "TYPE2SET",
                                            "width": 500,
                                            "rows": 1
                                        },
                                        {
                                            "name": "scrollableMessageBody",
                                            "characterSet": "TYPE2SET",
                                            "width": 500,
                                            "rows": 1
                                        },
                                        {
                                            "name": "initialInteractionText",
                                            "characterSet": "TYPE2SET",
                                            "width": 500,
                                            "rows": 1
                                        },
                                        {
                                            "name": "navigationText1",
                                            "characterSet": "TYPE2SET",
                                            "width": 500,
                                            "rows": 1
                                        },
                                        {
                                            "name": "navigationText2",
                                            "characterSet": "TYPE2SET",
                                            "width": 500,
                                            "rows": 1
                                        },
                                        {
                                            "name": "ETA",
                                            "characterSet": "TYPE2SET",
                                            "width": 500,
                                            "rows": 1
                                        },
                                        {
                                            "name": "totalDistance",
                                            "characterSet": "TYPE2SET",
                                            "width": 500,
                                            "rows": 1
                                        },
                                        {
                                            "name": "navigationText",
                                            "characterSet": "TYPE2SET",
                                            "width": 500,
                                            "rows": 1
                                        },
                                        {
                                            "name": "audioPassThruDisplayText1",
                                            "characterSet": "TYPE2SET",
                                            "width": 500,
                                            "rows": 1
                                        },
                                        {
                                            "name": "audioPassThruDisplayText2",
                                            "characterSet": "TYPE2SET",
                                            "width": 500,
                                            "rows": 1
                                        },
                                        {
                                            "name": "sliderHeader",
                                            "characterSet": "TYPE2SET",
                                            "width": 500,
                                            "rows": 1
                                        },
                                        {
                                            "name": "sliderFooter",
                                            "characterSet": "TYPE2SET",
                                            "width": 500,
                                            "rows": 1
                                        },
                                        {
                                            "name": "notificationText",
                                            "characterSet": "TYPE2SET",
                                            "width": 500,
                                            "rows": 1
                                        },
                                        {
                                            "name": "menuName",
                                            "characterSet": "TYPE2SET",
                                            "width": 500,
                                            "rows": 1
                                        },
                                        {
                                            "name": "secondaryText",
                                            "characterSet": "TYPE2SET",
                                            "width": 500,
                                            "rows": 1
                                        },
                                        {
                                            "name": "tertiaryText",
                                            "characterSet": "TYPE2SET",
                                            "width": 500,
                                            "rows": 1
                                        },
                                        {
                                            "name": "timeToDestination",
                                            "characterSet": "TYPE2SET",
                                            "width": 500,
                                            "rows": 1
                                        },
                                        {
                                            "name": "turnText",
                                            "characterSet": "TYPE2SET",
                                            "width": 500,
                                            "rows": 1
                                        },
                                        {
                                            "name": "menuTitle",
                                            "characterSet": "TYPE2SET",
                                            "width": 500,
                                            "rows": 1
                                        },
                                        {
                                            "name": "locationName",
                                            "characterSet": "TYPE2SET",
                                            "width": 500,
                                            "rows": 1
                                        },
                                        {
                                            "name": "locationDescription",
                                            "characterSet": "TYPE2SET",
                                            "width": 500,
                                            "rows": 1
                                        },
                                        {
                                            "name": "addressLines",
                                            "characterSet": "TYPE2SET",
                                            "width": 500,
                                            "rows": 1
                                        },
                                        {
                                            "name": "phoneNumber",
                                            "characterSet": "TYPE2SET",
                                            "width": 500,
                                            "rows": 1
                                        }
                                    ],
                                    "imageFields": [
                                        {
                                            "name": "softButtonImage",
                                            "imageTypeSupported":
                                                [
                                                    "GRAPHIC_BMP",
                                                    "GRAPHIC_JPEG",
                                                    "GRAPHIC_PNG"
                                                ],
                                            "imageResolution":
                                            {
                                                "resolutionWidth": 64,
                                                "resolutionHeight": 64
                                            }
                                        },
                                        {
                                            "name": "choiceImage",
                                            "imageTypeSupported":
                                                [
                                                    "GRAPHIC_BMP",
                                                    "GRAPHIC_JPEG",
                                                    "GRAPHIC_PNG"
                                                ],
                                            "imageResolution":
                                            {
                                                "resolutionWidth": 64,
                                                "resolutionHeight": 64
                                            }
                                        },
                                        {
                                            "name": "choiceSecondaryImage",
                                            "imageTypeSupported":
                                                [
                                                    "GRAPHIC_BMP",
                                                    "GRAPHIC_JPEG",
                                                    "GRAPHIC_PNG"
                                                ],
                                            "imageResolution":
                                            {
                                                "resolutionWidth": 64,
                                                "resolutionHeight": 64
                                            }
                                        },
                                        {
                                            "name": "vrHelpItem",
                                            "imageTypeSupported":
                                                [
                                                    "GRAPHIC_BMP",
                                                    "GRAPHIC_JPEG",
                                                    "GRAPHIC_PNG"
                                                ],
                                            "imageResolution":
                                            {
                                                "resolutionWidth": 64,
                                                "resolutionHeight": 64
                                            }
                                        },
                                        {
                                            "name": "turnIcon",
                                            "imageTypeSupported":
                                                [
                                                    "GRAPHIC_BMP",
                                                    "GRAPHIC_JPEG",
                                                    "GRAPHIC_PNG"
                                                ],
                                            "imageResolution":
                                            {
                                                "resolutionWidth": 64,
                                                "resolutionHeight": 64
                                            }
                                        },
                                        {
                                            "name": "menuIcon",
                                            "imageTypeSupported":
                                                [
                                                    "GRAPHIC_BMP",
                                                    "GRAPHIC_JPEG",
                                                    "GRAPHIC_PNG"
                                                ],
                                            "imageResolution":
                                            {
                                                "resolutionWidth": 64,
                                                "resolutionHeight": 64
                                            }
                                        },
                                        {
                                            "name": "cmdIcon",
                                            "imageTypeSupported":
                                                [
                                                    "GRAPHIC_BMP",
                                                    "GRAPHIC_JPEG",
                                                    "GRAPHIC_PNG"
                                                ],
                                            "imageResolution":
                                            {
                                                "resolutionWidth": 64,
                                                "resolutionHeight": 64
                                            }
                                        },
                                        {
                                            "name": "graphic",
                                            "imageTypeSupported":
                                                [
                                                    "GRAPHIC_BMP",
                                                    "GRAPHIC_JPEG",
                                                    "GRAPHIC_PNG"
                                                ],
                                            "imageResolution":
                                            {
                                                "resolutionWidth": 64,
                                                "resolutionHeight": 64
                                            }
                                        },
                                        {
                                            "name": "showConstantTBTIcon",
                                            "imageTypeSupported":
                                                [
                                                    "GRAPHIC_BMP",
                                                    "GRAPHIC_JPEG",
                                                    "GRAPHIC_PNG"
                                                ],
                                            "imageResolution":
                                            {
                                                "resolutionWidth": 64,
                                                "resolutionHeight": 64
                                            }
                                        },
                                        {
                                            "name": "showConstantTBTNextTurnIcon",
                                            "imageTypeSupported":
                                                [
                                                    "GRAPHIC_BMP",
                                                    "GRAPHIC_JPEG",
                                                    "GRAPHIC_PNG"
                                                ],
                                            "imageResolution":
                                            {
                                                "resolutionWidth": 64,
                                                "resolutionHeight": 64
                                            }
                                        },
                                        {
                                            "name": "showConstantTBTNextTurnIcon",
                                            "imageTypeSupported":
                                                [
                                                    "GRAPHIC_BMP",
                                                    "GRAPHIC_JPEG",
                                                    "GRAPHIC_PNG"
                                                ],
                                            "imageResolution":
                                            {
                                                "resolutionWidth": 64,
                                                "resolutionHeight": 64
                                            }
                                        }
                                    ],
                                    "mediaClockFormats": [
                                        "CLOCK1", "CLOCK2", "CLOCK3", "CLOCKTEXT1", "CLOCKTEXT2", "CLOCKTEXT3", "CLOCKTEXT4"
                                    ],
                                    "graphicSupported": true,
                                    "imageCapabilities": ["DYNAMIC", "STATIC"],
                                    "templatesAvailable": [request.params.displayLayout],
                                    "screenParams": {
                                        "resolution": {
                                            "resolutionWidth": 800,
                                            "resolutionHeight": 480
                                        },
                                        "touchEventAvailable": {
                                            "pressAvailable": true,
                                            "multiTouchAvailable": true,
                                            "doublePressAvailable": false
                                        }
                                    },
                                    "numCustomPresetsAvailable": 10
                                },
                                "buttonCapabilities": [
                                    {
                                        "name": "PRESET_0",
                                        "shortPressAvailable": true,
                                        "longPressAvailable": true,
                                        "upDownAvailable": true
                                    }, {
                                        "name": "PRESET_1",
                                        "shortPressAvailable": true,
                                        "longPressAvailable": true,
                                        "upDownAvailable": true
                                    }, {
                                        "name": "PRESET_2",
                                        "shortPressAvailable": true,
                                        "longPressAvailable": true,
                                        "upDownAvailable": true
                                    }, {
                                        "name": "PRESET_3",
                                        "shortPressAvailable": true,
                                        "longPressAvailable": true,
                                        "upDownAvailable": true
                                    }, {
                                        "name": "PRESET_4",
                                        "shortPressAvailable": true,
                                        "longPressAvailable": true,
                                        "upDownAvailable": true
                                    }, {
                                        "name": "PRESET_5",
                                        "shortPressAvailable": true,
                                        "longPressAvailable": true,
                                        "upDownAvailable": true
                                    }, {
                                        "name": "PRESET_6",
                                        "shortPressAvailable": true,
                                        "longPressAvailable": true,
                                        "upDownAvailable": true
                                    }, {
                                        "name": "PRESET_7",
                                        "shortPressAvailable": true,
                                        "longPressAvailable": true,
                                        "upDownAvailable": true
                                    }, {
                                        "name": "PRESET_8",
                                        "shortPressAvailable": true,
                                        "longPressAvailable": true,
                                        "upDownAvailable": true
                                    }, {
                                        "name": "PRESET_9",
                                        "shortPressAvailable": true,
                                        "longPressAvailable": true,
                                        "upDownAvailable": true
                                    }, {
                                        "name": "OK",
                                        "shortPressAvailable": true,
                                        "longPressAvailable": true,
                                        "upDownAvailable": true
                                    }, {
                                        "name": "SEEKLEFT",
                                        "shortPressAvailable": true,
                                        "longPressAvailable": true,
                                        "upDownAvailable": true
                                    }, {
                                        "name": "SEEKRIGHT",
                                        "shortPressAvailable": true,
                                        "longPressAvailable": true,
                                        "upDownAvailable": true
                                    }, {
                                        "name": "TUNEUP",
                                        "shortPressAvailable": true,
                                        "longPressAvailable": true,
                                        "upDownAvailable": true
                                    }, {
                                        "name": "TUNEDOWN",
                                        "shortPressAvailable": true,
                                        "longPressAvailable": true,
                                        "upDownAvailable": true
                                    }
                                ],
                                "softButtonCapabilities": [{
                                    "shortPressAvailable": true,
                                    "longPressAvailable": true,
                                    "upDownAvailable": true,
                                    "imageSupported": true
                                }],
                                "presetBankCapabilities": {
                                    "onScreenPresetsAvailable": true
                                },
                                "code": SDL.SDLModel.resultCode["SUCCESS"],
                                "method": "UI.SetDisplayLayout"
                            }
                        };

                        this.client.send(JSONMessage);

                    } else {
                        this.sendError(SDL.SDLModel.resultCode["UNSUPPORTED_RESOURCE"], request.id, request.method, "Unsupported display layout!")
                    }

                    break;
                }
                case "UI.SetAppIcon":
                {

                    // Werify if there is an ansupported data in request
                    if (this.errorResponsePull[request.id] != null) {

                        //Check if there is any available data to  process the request
                        if (!("syncFileName" in request.params)) {

                            this.sendError(this.errorResponsePull[request.id].code, request.id, request.method,
                                    "Unsupported " + this.errorResponsePull[request.id].type + " type. Request was not processed.");
                            this.errorResponsePull[request.id] = null;

                            return;
                        }
                    }

                    SDL.SDLModel.onSDLSetAppIcon(request.params, request.id, request.method);

                    break;
                }
                case "UI.PerformAudioPassThru":
                {

                    if (this.performAudioPassThruRequestID > 0) {
                        this.sendError(
                            SDL.SDLModel.resultCode["REJECTED"],
                            request.id,
                            request.method,
                            'PerformAudioPassThru request aborted!');
                    } else {

                        this.performAudioPassThruRequestID = request.id;
                        SDL.SDLModel.UIPerformAudioPassThru(request.params);

                        SDL.SDLController.onSystemContextChange();
                    }

                    break;
                }
                case "UI.EndAudioPassThru":
                {

                    this.endAudioPassThruRequestID = request.id;

                    SDL.SDLModel.UIEndAudioPassThru();

                    break;
                }
                case "UI.GetSupportedLanguages":
                {

                    Em.Logger.log("FFW." + request.method + "Response");

                    var JSONMessage = {
                        "id": request.id,
                        "jsonrpc": "2.0",
                        "result": {
                            "code": SDL.SDLModel.resultCode["SUCCESS"], // type
                            // (enum)
                            // from
                            // SDL
                            "method": "UI.GetSupportedLanguages",
                            "languages": SDL.SDLModel.sdlLanguagesList
                        }
                    };
                    this.client.send(JSONMessage);

                    break;
                }
                case "UI.GetLanguage":
                {

                    Em.Logger.log("FFW." + request.method + "Response");

                    var JSONMessage = {
                        "jsonrpc": "2.0",
                        "id": request.id,
                        "result": {
                            "code": SDL.SDLModel.resultCode["SUCCESS"], // type
                            // (enum)
                            // from
                            // SDL
                            "method": "UI.GetLanguage",
                            "language": SDL.SDLModel.hmiUILanguage
                        }
                    };
                    this.client.send(JSONMessage);

                    break;
                }
                case "UI.GetCapabilities":
                {

                    Em.Logger.log("FFW." + request.method + "Response");
                    // send repsonse
                    var JSONMessage = {
                        "jsonrpc": "2.0",
                        "id": request.id,
                        "result": {
                            "displayCapabilities": {
                                "displayType": "GEN2_8_DMA",
                                "textFields": [{
                                        "name": "mainField1",
                                        "characterSet": "TYPE2SET",
                                        "width": 500,
                                        "rows": 1
                                    },
                                    {
                                        "name": "mainField2",
                                        "characterSet": "TYPE2SET",
                                        "width": 500,
                                        "rows": 1
                                    },
                                    {
                                        "name": "mainField3",
                                        "characterSet": "TYPE2SET",
                                        "width": 500,
                                        "rows": 1
                                    },
                                    {
                                        "name": "mainField4",
                                        "characterSet": "TYPE2SET",
                                        "width": 500,
                                        "rows": 1
                                    },
                                    {
                                        "name": "statusBar",
                                        "characterSet": "TYPE2SET",
                                        "width": 500,
                                        "rows": 1
                                    },
                                    {
                                        "name": "mediaClock",
                                        "characterSet": "TYPE2SET",
                                        "width": 500,
                                        "rows": 1
                                    },
                                    {
                                        "name": "mediaTrack",
                                        "characterSet": "TYPE2SET",
                                        "width": 500,
                                        "rows": 1
                                    },
                                    {
                                        "name": "alertText1",
                                        "characterSet": "TYPE2SET",
                                        "width": 500,
                                        "rows": 1
                                    },
                                    {
                                        "name": "alertText2",
                                        "characterSet": "TYPE2SET",
                                        "width": 500,
                                        "rows": 1
                                    },
                                    {
                                        "name": "alertText3",
                                        "characterSet": "TYPE2SET",
                                        "width": 500,
                                        "rows": 1
                                    },
                                    {
                                        "name": "scrollableMessageBody",
                                        "characterSet": "TYPE2SET",
                                        "width": 500,
                                        "rows": 1
                                    },
                                    {
                                        "name": "initialInteractionText",
                                        "characterSet": "TYPE2SET",
                                        "width": 500,
                                        "rows": 1
                                    },
                                    {
                                        "name": "navigationText1",
                                        "characterSet": "TYPE2SET",
                                        "width": 500,
                                        "rows": 1
                                    },
                                    {
                                        "name": "navigationText2",
                                        "characterSet": "TYPE2SET",
                                        "width": 500,
                                        "rows": 1
                                    },
                                    {
                                        "name": "ETA",
                                        "characterSet": "TYPE2SET",
                                        "width": 500,
                                        "rows": 1
                                    },
                                    {
                                        "name": "totalDistance",
                                        "characterSet": "TYPE2SET",
                                        "width": 500,
                                        "rows": 1
                                    },
                                    {
                                        "name": "navigationText",
                                        "characterSet": "TYPE2SET",
                                        "width": 500,
                                        "rows": 1
                                    },
                                    {
                                        "name": "audioPassThruDisplayText1",
                                        "characterSet": "TYPE2SET",
                                        "width": 500,
                                        "rows": 1
                                    },
                                    {
                                        "name": "audioPassThruDisplayText2",
                                        "characterSet": "TYPE2SET",
                                        "width": 500,
                                        "rows": 1
                                    },
                                    {
                                        "name": "sliderHeader",
                                        "characterSet": "TYPE2SET",
                                        "width": 500,
                                        "rows": 1
                                    },
                                    {
                                        "name": "sliderFooter",
                                        "characterSet": "TYPE2SET",
                                        "width": 500,
                                        "rows": 1
                                    },
                                    {
                                        "name": "notificationText",
                                        "characterSet": "TYPE2SET",
                                        "width": 500,
                                        "rows": 1
                                    },
                                    {
                                        "name": "menuName",
                                        "characterSet": "TYPE2SET",
                                        "width": 500,
                                        "rows": 1
                                    },
                                    {
                                        "name": "secondaryText",
                                        "characterSet": "TYPE2SET",
                                        "width": 500,
                                        "rows": 1
                                    },
                                    {
                                        "name": "tertiaryText",
                                        "characterSet": "TYPE2SET",
                                        "width": 500,
                                        "rows": 1
                                    },
                                    {
                                        "name": "timeToDestination",
                                        "characterSet": "TYPE2SET",
                                        "width": 500,
                                        "rows": 1
                                    },
                                    {
                                        "name": "turnText",
                                        "characterSet": "TYPE2SET",
                                        "width": 500,
                                        "rows": 1
                                    },
                                    {
                                        "name": "menuTitle",
                                        "characterSet": "TYPE2SET",
                                        "width": 500,
                                        "rows": 1
                                    },
                                    {
                                        "name": "locationName",
                                        "characterSet": "TYPE2SET",
                                        "width": 500,
                                        "rows": 1
                                    },
                                    {
                                        "name": "locationDescription",
                                        "characterSet": "TYPE2SET",
                                        "width": 500,
                                        "rows": 1
                                    },
                                    {
                                        "name": "addressLines",
                                        "characterSet": "TYPE2SET",
                                        "width": 500,
                                        "rows": 1
                                    },
                                    {
                                        "name": "phoneNumber",
                                        "characterSet": "TYPE2SET",
                                        "width": 500,
                                        "rows": 1
                                    }
                                ],
                                "imageFields": [
                                    {
                                        "name": "softButtonImage",
                                        "imageTypeSupported":
                                            [
                                                "GRAPHIC_BMP",
                                                "GRAPHIC_JPEG",
                                                "GRAPHIC_PNG"
                                            ],
                                        "imageResolution":
                                        {
                                            "resolutionWidth": 64,
                                            "resolutionHeight": 64
                                        }
                                    },
                                    {
                                        "name": "choiceImage",
                                        "imageTypeSupported":
                                            [
                                                "GRAPHIC_BMP",
                                                "GRAPHIC_JPEG",
                                                "GRAPHIC_PNG"
                                            ],
                                        "imageResolution":
                                        {
                                            "resolutionWidth": 64,
                                            "resolutionHeight": 64
                                        }
                                    },
                                    {
                                        "name": "choiceSecondaryImage",
                                        "imageTypeSupported":
                                            [
                                                "GRAPHIC_BMP",
                                                "GRAPHIC_JPEG",
                                                "GRAPHIC_PNG"
                                            ],
                                        "imageResolution":
                                        {
                                            "resolutionWidth": 64,
                                            "resolutionHeight": 64
                                        }
                                    },
                                    {
                                        "name": "vrHelpItem",
                                        "imageTypeSupported":
                                            [
                                                "GRAPHIC_BMP",
                                                "GRAPHIC_JPEG",
                                                "GRAPHIC_PNG"
                                            ],
                                        "imageResolution":
                                        {
                                            "resolutionWidth": 64,
                                            "resolutionHeight": 64
                                        }
                                    },
                                    {
                                        "name": "turnIcon",
                                        "imageTypeSupported":
                                            [
                                                "GRAPHIC_BMP",
                                                "GRAPHIC_JPEG",
                                                "GRAPHIC_PNG"
                                            ],
                                        "imageResolution":
                                        {
                                            "resolutionWidth": 64,
                                            "resolutionHeight": 64
                                        }
                                    },
                                    {
                                        "name": "menuIcon",
                                        "imageTypeSupported":
                                            [
                                                "GRAPHIC_BMP",
                                                "GRAPHIC_JPEG",
                                                "GRAPHIC_PNG"
                                            ],
                                        "imageResolution":
                                        {
                                            "resolutionWidth": 64,
                                            "resolutionHeight": 64
                                        }
                                    },
                                    {
                                        "name": "cmdIcon",
                                        "imageTypeSupported":
                                            [
                                                "GRAPHIC_BMP",
                                                "GRAPHIC_JPEG",
                                                "GRAPHIC_PNG"
                                            ],
                                        "imageResolution":
                                        {
                                            "resolutionWidth": 64,
                                            "resolutionHeight": 64
                                        }
                                    },
                                    {
                                        "name": "graphic",
                                        "imageTypeSupported":
                                            [
                                                "GRAPHIC_BMP",
                                                "GRAPHIC_JPEG",
                                                "GRAPHIC_PNG"
                                            ],
                                        "imageResolution":
                                        {
                                            "resolutionWidth": 64,
                                            "resolutionHeight": 64
                                        }
                                    },
                                    {
                                        "name": "showConstantTBTIcon",
                                        "imageTypeSupported":
                                            [
                                                "GRAPHIC_BMP",
                                                "GRAPHIC_JPEG",
                                                "GRAPHIC_PNG"
                                            ],
                                        "imageResolution":
                                        {
                                            "resolutionWidth": 64,
                                            "resolutionHeight": 64
                                        }
                                    },
                                    {
                                        "name": "showConstantTBTNextTurnIcon",
                                        "imageTypeSupported":
                                            [
                                                "GRAPHIC_BMP",
                                                "GRAPHIC_JPEG",
                                                "GRAPHIC_PNG"
                                            ],
                                        "imageResolution":
                                        {
                                            "resolutionWidth": 64,
                                            "resolutionHeight": 64
                                        }
                                    },
                                    {
                                        "name": "showConstantTBTNextTurnIcon",
                                        "imageTypeSupported":
                                            [
                                                "GRAPHIC_BMP",
                                                "GRAPHIC_JPEG",
                                                "GRAPHIC_PNG"
                                            ],
                                        "imageResolution":
                                        {
                                            "resolutionWidth": 64,
                                            "resolutionHeight": 64
                                        }
                                    }
                                ],
                                "mediaClockFormats": [
                                    "CLOCK1", "CLOCK2", "CLOCK3", "CLOCKTEXT1", "CLOCKTEXT2", "CLOCKTEXT3", "CLOCKTEXT4"
                                ],
                                "graphicSupported": true,
                                "imageCapabilities": ["DYNAMIC", "STATIC"],
                                "templatesAvailable": ["TEMPLATE"],
                                "screenParams": {
                                    "resolution": {
                                        "resolutionWidth": 800,
                                        "resolutionHeight": 480
                                    },
                                    "touchEventAvailable": {
                                        "pressAvailable": true,
                                        "multiTouchAvailable": true,
                                        "doublePressAvailable": false
                                    }
                                },
                                "numCustomPresetsAvailable": 10
                            },
                            "audioPassThruCapabilities": {
                                "samplingRate": "44KHZ",
                                "bitsPerSample": "8_BIT",
                                "audioType": "PCM"
                            },
                            "hmiZoneCapabilities": "FRONT",
                            "softButtonCapabilities": {
                                "shortPressAvailable": true,
                                "longPressAvailable": true,
                                "upDownAvailable": true,
                                "imageSupported": true
                            },
                            "code": SDL.SDLModel.resultCode["SUCCESS"],
                            "method": "UI.GetCapabilities"
                        }
                    };

                    this.client.send(JSONMessage);

                    break;
                }
                case "UI.IsReady":
                {

                    Em.Logger.log("FFW." + request.method + "Response");
                    // send repsonse
                    var JSONMessage = {
                        "jsonrpc": "2.0",
                        "id": request.id,
                        "result": {
                            "available": this.get('isReady'),
                            "code": SDL.SDLModel.resultCode["SUCCESS"],
                            "method": "UI.IsReady"
                        }
                    };

                    this.client.send(JSONMessage);

                    break;
                }
                case "UI.ClosePopUp":
                {

                    SDL.SDLController.closePopUp(request.params.methodName);


                    Em.Logger.log("FFW." + request.method + "Response");
                    // send repsonse
                    var JSONMessage = {
                        "jsonrpc": "2.0",
                        "id": request.id,
                        "result": {
                            "code": SDL.SDLModel.resultCode["SUCCESS"],
                            "method": "UI.ClosePopUp"
                        }
                    };

                    this.client.send(JSONMessage);

                    break;
                }
                case "UI.ShowVrHelp":
                {

                    //SDL.SDLModel.ShowVrHelp(request.params);

                    this.sendUIResult(SDL.SDLModel.resultCode["SUCCESS"], request.id, request.method);

                    break;
                }
                default:
                {
                    // statements_def
                    break;
                }
            }
        }
    },

    /**
     * Send error response from onRPCRequest
     *
     * @param {Number}
     *            resultCode
     * @param {Number}
     *            id
     * @param {String}
     *            method
     */
    sendError: function (resultCode, id, method, message) {

        Em.Logger.log("FFW." + method + "Response");

        if (resultCode !== 0) {

            // send repsonse
            var JSONMessage = {
                "jsonrpc": "2.0",
                "id": id,
                "error": {
                    "code": resultCode, // type (enum) from SDL protocol
                    "message": message,
                    "data": {
                        "method": method
                    }
                }
            };
            this.client.send(JSONMessage);
        }
    },

    /**
     * send response from onRPCRequest
     *
     * @param {Number}
     *            resultCode
     * @param {Number}
     *            id
     * @param {String}
     *            method
     */
    sendUIResult: function (resultCode, id, method) {

        if (this.errorResponsePull[id]) {

            this.sendError(this.errorResponsePull[id].code, id, method,
                    "Unsupported " + this.errorResponsePull[id].type + " type. Available data in request was processed.");
            this.errorResponsePull[id] = null;
            return;
        }

        Em.Logger.log("FFW." + method + "Response");

        if (resultCode === SDL.SDLModel.resultCode["SUCCESS"]) {

            // send repsonse
            var JSONMessage = {
                "jsonrpc": "2.0",
                "id": id,
                "result": {
                    "code": resultCode, // type (enum) from SDL protocol
                    "method": method
                }
            };
            this.client.send(JSONMessage);
        }
    },

    /**
     * send response from onRPCRequest
     *
     * @param {Number}
     *            resultCode
     * @param {Number}
     *            id
     */
    alertResponse: function (resultCode, id) {

        Em.Logger.log("FFW.UI.AlertResponse");

        switch (resultCode) {
            case SDL.SDLModel.resultCode["SUCCESS"]: {

                this.sendUIResult(resultCode, id, 'UI.Alert');

                break;
            }
            case SDL.SDLModel.resultCode["ABORTED"]: {

                this.sendError(resultCode, id, "UI.Alert", 'Alert request aborted.');
                break;
            }
            case SDL.SDLModel.resultCode["REJECTED"]: {

                this.sendError(resultCode, id, "UI.Alert", 'Another Alert is active.');
                break;
            }
        }
    },

    /**
     * send response from onRPCRequest
     *
     * @param {Number}
     *            resultCode
     * @param {Number}
     *            sliderRequestID
     * @param {Number}
     *            sliderPosition
     */
    sendSliderResult: function (resultCode, sliderRequestID, sliderPosition) {


        Em.Logger.log("FFW.UI.SliderResponse");

        if (resultCode === SDL.SDLModel.resultCode["SUCCESS"]) {

            this.sendUIResult(resultCode, id, 'UI.Alert');
            // send repsonse
            var JSONMessage = {
                "jsonrpc": "2.0",
                "id": sliderRequestID,
                "result": {
                    "code": resultCode, // type (enum) from SDL protocol
                    "method": 'UI.Slider'
                }
            };

            if (sliderPosition) {
                JSONMessage.result.sliderPosition = sliderPosition;
            }
        } else {
            // send repsonse
            var JSONMessage = {
                "jsonrpc": "2.0",
                "id": sliderRequestID,
                "error": {
                    "code": resultCode, // type (enum) from SDL protocol
                    "message": 'Slider request ABORTED or TIMED OUT.',
                    "data": {
                        "method": 'UI.Slider'
                    }
                }
            };

            if (sliderPosition) {
                JSONMessage.error.data.sliderPosition = sliderPosition;
            }
        }

        this.client.send(JSONMessage);
    },

    /**
     * Notification method to send touch event data to SDLCore
     *
     * @param {Number} appID
     * @param {String} methodName
     */
    onResetTimeout: function (appID, methodName) {

        Em.Logger.log("FFW.UI.OnResetTimeout");

        var JSONMessage = {
            "jsonrpc": "2.0",
            "method": "UI.OnResetTimeout",
            "params": {
                "methodName": methodName,
                "appID": appID
            }
        };
        this.client.send(JSONMessage);
    },

    /**
     * send notification when command was triggered
     *
     * @param {Number}
     *            commandID
     * @param {Number}
     *            appID
     */
    onCommand: function (commandID, appID) {

        Em.Logger.log("FFW.UI.onCommand");

        var JSONMessage = {
            "jsonrpc": "2.0",
            "method": "UI.OnCommand",
            "params": {
                "cmdID": commandID,
                "appID": appID
            }
        };
        this.client.send(JSONMessage);
    },

    /**
     * Notification method to send touch event data to SDLCore
     *
     * @param {String} type
     * @param {Object} touchLists
     * @param {Object} info
     */
    onTouchEvent: function (type, event) {

        Em.Logger.log("FFW.UI.OnTouchEvent");

        var JSONMessage = {
            "jsonrpc": "2.0",
            "method": "UI.OnTouchEvent",
            "params": {
                "type": type,
                "event": event
            }
        };
        this.client.send(JSONMessage);
    },

    /**
     * send notification when command was triggered
     *
     * @param {Number}
     *            softButtonID
     * @param {Number}
     *            appID
     */
    onCommandSoftButton: function (softButtonID, appID) {

        Em.Logger.log("FFW.UI.onCommand");

        var JSONMessage = {
            "jsonrpc": "2.0",
            "method": "UI.OnCommand",
            "params": {
                "commandID": softButtonID,
                "appID": appID
            }
        };
        this.client.send(JSONMessage);
    },

    /**
     * send notification when command was triggered
     *
     * @param {Number} requestID
     * @param {Number} resultCode
     * @param {Number} commandID
     * @param {String} manualTextEntry
     */
    interactionResponse: function (requestID, resultCode, commandID, manualTextEntry) {

        Em.Logger.log("FFW.UI.PerformInteractionResponse");

        if (this.errorResponsePull[requestID]) {

            // send repsonse
            var JSONMessage = {
                "jsonrpc": "2.0",
                "id": requestID,
                "error": {
                    "code": this.errorResponsePull[requestID].code,
                    "message": "Unsupported " + this.errorResponsePull[requestID].type
                    + " type. Available data in request was processed.",
                    "data": {
                        "method": "UI.PerformInteraction"
                    }
                }
            };

            if (commandID) {
                JSONMessage.error.data.choiceID = commandID;
            }

            if (manualTextEntry != null) {
                JSONMessage.error.data.manualTextEntry = manualTextEntry;
            }

            this.client.send(JSONMessage);
            this.errorResponsePull[requestID] = null;
            return;
        }

        if (resultCode === SDL.SDLModel.resultCode["SUCCESS"]) {
            // send repsonse
            var JSONMessage = {
                "jsonrpc": "2.0",
                "id": requestID,
                "result": {
                    "code": resultCode,
                    "method": "UI.PerformInteraction"
                }
            };

            if (commandID) {
                JSONMessage.result.choiceID = commandID;
            }

            if (manualTextEntry != null) {
                JSONMessage.result.manualTextEntry = manualTextEntry;
            }
        } else {
            // send repsonse
            var JSONMessage = {
                "jsonrpc": "2.0",
                "id": requestID,
                "error": {
                    "code": resultCode, // type (enum) from SDL protocol
                    "message": "Perform Interaction error response.",
                    "data": {
                        "method": "UI.PerformInteraction"
                    }
                }
            };
        }

        this.client.send(JSONMessage);
    },

    /**
     * send notification when DriverDistraction PopUp is visible
     *
     * @param {String}
     *            driverDistractionState
     */
    onDriverDistraction: function (driverDistractionState) {

        Em.Logger.log("FFW.UI.DriverDistraction");

        // send repsonse
        var JSONMessage = {
            "jsonrpc": "2.0",
            "method": "UI.OnDriverDistraction",
            "params": {
                "state": driverDistractionState
            }
        };
        this.client.send(JSONMessage);
    },

    /**
     * Notifies if system context is changed
     *
     * @param {String}
     *            systemContextValue
     */
    OnSystemContext: function (systemContextValue, appID) {

        Em.Logger.log("FFW.UI.OnSystemContext");

        // send repsonse
        var JSONMessage = {
            "jsonrpc": "2.0",
            "method": "UI.OnSystemContext",
            "params": {
                "systemContext": systemContextValue
            }
        };

        if (appID) {
            JSONMessage.params.appID = appID;
        }

        this.client.send(JSONMessage);
    },

    /**
     * Notifies if sdl UI components language was changed
     *
     * @param {String}
     *            lang
     */
    OnLanguageChange: function (lang) {

        Em.Logger.log("FFW.UI.OnLanguageChange");

        // send repsonse
        var JSONMessage = {
            "jsonrpc": "2.0",
            "method": "UI.OnLanguageChange",
            "params": {
                "language": lang
            }
        };
        this.client.send(JSONMessage);
    },

    /**
     *  Sends notification on SDL Core display keyboard value
     *
     * @param {String}
     *
     */
    OnKeyboardInput: function (value, event) {

        Em.Logger.log("FFW.UI.OnKeyboardInput");

        // send repsonse
        var JSONMessage = {
            "jsonrpc": "2.0",
            "method": "UI.OnKeyboardInput",
            "params": {
                "data": value,
                "event": event
            }
        };
        this.client.send(JSONMessage);
    }
})
